﻿#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QSerialPortInfo>

#include <QFileDialog>
#include <QSettings>

#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);


    RefreshPort();
    RefreshCmdPort();

    connect(ui->pushButton_ok,SIGNAL(clicked()),this,SLOT(BtnOKClickedSlot()));
    connect(ui->pushButton_open,SIGNAL(clicked()),this,SLOT(BtnOpenFileClickedSlot()));
    connect(ui->pushButton_portRefresh,SIGNAL(clicked()),this,SLOT(BtnPortClickedSlot()));
    connect(ui->pushButton_portRefresh_cmd,SIGNAL(clicked()),this,SLOT(BtnCmdPortClickedSlot()));

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::RefreshPort()
{
    ui->comboBox_Port->clear();

    foreach (QSerialPortInfo info ,QSerialPortInfo::availablePorts())
    {
        ui->comboBox_Port->insertItem(0, info.portName());
    }
}

void MainWindow::RefreshCmdPort()
{
    ui->comboBox_Port_cmd->clear();

    foreach (QSerialPortInfo info ,QSerialPortInfo::availablePorts())
    {
        ui->comboBox_Port_cmd->insertItem(0, info.portName());
    }
}

void MainWindow::BtnOpenFileClickedSlot()
{

    QString path = qApp->applicationDirPath();
    m_szFileName  = QFileDialog::getOpenFileName(this, tr("Open"), path,
                               tr("settings files (*.ini);;All files (*.*)"));
    if (m_szFileName != "")
    {
        QSettings set(m_szFileName, QSettings::IniFormat);
        set.setIniCodec("UTF-8");


        ui->lineEdit_filename->setText(m_szFileName);
        int index = ui->comboBox_Port->findText(set.value("GCodeProt").toString());
        if(index != -1)
        {
          ui->comboBox_Port->setCurrentIndex(index);
        }
        index = ui->comboBox_baud->findText(set.value("GCodebaud").toString());
        if(index != -1)
        {
            ui->comboBox_baud->setCurrentIndex(index);
        }

        index = ui->comboBox_Port_cmd->findText(set.value("cmdProt").toString());
        if(index != -1)
        {
          ui->comboBox_Port_cmd->setCurrentIndex(index);
        }
        index = ui->comboBox_baud_cmd->findText(set.value("cmdbaud").toString());
        if(index != -1)
        {
            ui->comboBox_baud_cmd->setCurrentIndex(index);
        }

        ui->spinBox_step->setValue(set.value("stepsize").toInt());

        ui->spinBox_speed->setValue(set.value("speed").toInt());

        ui->comboBox_time->setCurrentText(set.value("dataholdtime").toString());

        ui->spinBox_photoCamera->setValue(set.value("photocamera").toInt());
        ui->spinBox_videoCamera->setValue(set.value("videocamera").toInt());


    }
}

void MainWindow::BtnPortClickedSlot()
{
    RefreshPort();
}

void MainWindow::BtnCmdPortClickedSlot()
{

}

void MainWindow::BtnOKClickedSlot()
{
    QString settingsFileName = qApp->applicationDirPath();
    if(m_szFileName.isEmpty())
    {
       m_szFileName = (QFileDialog::getSaveFileName(this, tr("Save file as"), "settings.ini", tr("settings files (*.)")));
       if(m_szFileName.isEmpty())
       {
            QMessageBox::warning(this,"错误","请选择要保存的文件");
       }
    }


    QSettings set(m_szFileName, QSettings::IniFormat);
    set.setIniCodec("UTF-8");

    set.setValue("GCodeProt",ui->comboBox_Port->currentText());
    set.setValue("GCodebaud",ui->comboBox_Port->currentText());
    set.setValue("cmdProt",ui->comboBox_Port_cmd->currentText());
    set.setValue("cmdbaud",ui->comboBox_baud_cmd->currentText());
    set.setValue("dataholdtime",ui->comboBox_time->currentText());
    set.setValue("speed",ui->spinBox_speed->value());
    set.setValue("stepsize",ui->spinBox_step->value());
    set.setValue("photocamera",ui->spinBox_photoCamera->value());
    set.setValue("videocamera",ui->spinBox_videoCamera->value());




}
